<?php

abstract class Jf_Validate_Abstract implements Jf_Validate_Interface {

    protected $_messages = array();

    protected $_data = array();

    protected $_error = false;

    public function getMessages () {
        return $this -> _messages;
    }

    public function setMessage ($messageString, $messageKey = null) {
        $this -> _messages[$messageKey] = $messageString;
        return $this;
    }

    public function getFirstMessage () {
        return array_shift ($this -> _messages);
    }

    public function setMessages (array $messages) {
        foreach ($messages as $key => $message) {
            $this -> setMessage ($message, $key);
        }
        return $this;
    }

    public function setError ($errorMsg, $msgKey = null) {
        if (is_array ($errorMsg)) {
            $errorMsg = array_shift ($errorMsg);
        }
        $this -> setMessage ($errorMsg, $msgKey);
        $this -> _error = true;
    }

    public function __get ($property) {
        if ($property == 'value') {
            return $this -> _value;
        }
        throw new Jf_Exception ("No property exists by the name '$property'");
    }

    function __set ($key, $value) {
        $this -> _data[$key] = $value;
        return $this;
    }

    public function getEscaped ($fieldName = null) {
        if ($fieldName === null) {
            return $this -> _data;
        } else {
            return $this -> _data[$fieldName];
        }
    }

}